package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.pojo.dto.globeconfig.GlobeConfigAddDto;
import com.teacher.pojo.dto.globeconfig.GlobeConfigUpdateDto;
import com.teacher.pojo.entity.GlobeConfig;
import com.teacher.service.IGlobeConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 配置表 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-14
 */
@RestController
@RequestMapping("/pc/globe-config")
public class GlobeConfigController {

    @Autowired
    private IGlobeConfigService iGlobeConfigService;

    /**
     * 更新值
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("更新值 globeConfig_update")
    @PreAuthorize("hasAuthority('globeConfig_update')")
    public CommonResult<String> update(@RequestBody GlobeConfigUpdateDto dto) {
        GlobeConfig globeConfig = new GlobeConfig();
        globeConfig.setId(dto.getId());
        globeConfig.setValue(dto.getValue());
        globeConfig.setRemark(dto.getRemark());
        if (iGlobeConfigService.updateById(globeConfig)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 删除配置
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除配置 globeConfig_delete")
    @PreAuthorize("hasAuthority('globeConfig_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        iGlobeConfigService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }

    /**
     * 添加配置
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("添加配置 globeConfig_add")
    @PreAuthorize("hasAuthority('globeConfig_add')")
    public CommonResult<String> add(@RequestBody GlobeConfigAddDto dto) {
        if (iGlobeConfigService.getOne(new QueryWrapper<GlobeConfig>().lambda()
                .eq(GlobeConfig::getName, dto.getName()).last("limit 1")) != null) {
            return CommonResult.failed("配置名重复");
        }
        GlobeConfig globeConfig = new GlobeConfig();
        BeanUtils.copyProperties(dto, globeConfig);
        if (iGlobeConfigService.save(globeConfig)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 查询所有配置
     *
     * @return
     */
    @GetMapping
    @ApiOperation("查询所有配置 globeConfig_get")
    @PreAuthorize("hasAuthority('globeConfig_get')")
    public CommonResult<List<GlobeConfig>> getAll() {
        return CommonResult.success(iGlobeConfigService.list());
    }

    /**
     * 通过name查询
     *
     * @param name
     * @return
     */
    @GetMapping("/byName")
    @ApiOperation("通过name查询")
    @PreAuthorize("hasAuthority('globeConfig_get_byName')")
    public CommonResult<GlobeConfig> getByName(String name) {
        GlobeConfig globeConfig = iGlobeConfigService.getOne(new QueryWrapper<GlobeConfig>().lambda()
                .eq(GlobeConfig::getName, name).last("limit 1"));
        return CommonResult.success(globeConfig);
    }

}

